      
package com.haoxin.ai_course.common.utils;

import cn.hutool.crypto.digest.HMac;
import cn.hutool.crypto.digest.HmacAlgorithm;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * @author jacky
 * @date 2022/5/26 10:29
 */
@Slf4j
public class ClientSignUtils {

    /**
     * 生成签名
     */
    public static String sign(SortedMap<String, String> params, String accessSecret) {
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            sb.append(entry.getKey());
            sb.append("=");
            sb.append(entry.getValue());
            sb.append("&");
        }
        
        String data = sb.substring(0, sb.length() - 1);
        log.info("开始签名，拼接字符串："+data);
        HMac mac = new HMac(HmacAlgorithm.HmacSHA256, accessSecret.getBytes(StandardCharsets.UTF_8));
        return mac.digestHex(data);
    }

}

    